module lab2C{
	
	uses interface Boot;

	uses interface AlohaSend;
	uses interface AlohaReceive;
	uses interface AlohaState;
	
	uses interface Packet;
	uses interface Timer<TMilli> as myTimer;
	
	uses interface Leds;
}
implementation{
	
	//Globals
	uint8_t g_state = APP_START;
	myData_t g_data;
	
	uint8_t g_channel = ALOHA_CH;
	
	event void Boot.booted() 
	{
		//call myTimer.startOneShot(1000);
		call myTimer.startPeriodic(500);
	}
		
	event void myTimer.fired() 
	{
		uint8_t oldState = 0;
		error_t myError;
		
		atomic oldState = g_state;
		
		switch(g_state)
		{
			case APP_START:
				atomic g_state = APP_WAIT_STARTDONE;
				myError = call AlohaState.start();
				
				if(myError != SUCCESS){
					atomic g_state = oldState; 	
				}
			break; 
			case APP_STOP:
				atomic g_state = APP_WAIT_STOPDONE;
				myError = call AlohaState.stop();
				
				if(myError != SUCCESS){
					atomic g_state = oldState; 	
				}else{
					call myTimer.stop();
				}
			break;
			case APP_SET_CHANNEL:
				atomic g_state = APP_WAIT_CHDONE;
				myError = call AlohaState.setChannel(g_channel);
				
				if(myError != SUCCESS){
					atomic g_state = oldState; 	
				}
			break;
			case APP_SEND:
				atomic g_state = APP_WAIT_SNDDONE;
				myError = call AlohaSend.send(&g_data);
				
				if(myError != SUCCESS){
					atomic g_state = oldState;
					//call Leds.led0Toggle();
				}else{
					//call Leds.led1Toggle();
				}
			break;
			case APP_WAIT_STARTDONE:
			case APP_WAIT_STOPDONE:
			case APP_WAIT_CHDONE:
			case APP_WAIT_SNDDONE:
			case APP_WAIT_RCV:
			break;
			
		}
	}
	
	/*******************************************************************************************************************
	 * 
	 *   Aloha Interface
	 * 
	 ******************************************************************************************************************/
	event void AlohaSend.sendDone(error_t error){
		if (g_state == APP_WAIT_SNDDONE){
			if (error == SUCCESS){
				atomic g_state = APP_SEND;
				//call Leds.led1Toggle();			
			}else{
				//call Leds.led0Toggle();
			}
		}else{
			//call Leds.led0Toggle();	
		}
	}
	
	event void AlohaReceive.receiveDone(myData_t* msg, error_t error){
		call Leds.led0Toggle();
	}
		
	event void AlohaState.startDone(error_t error){
		if (g_state == APP_WAIT_STARTDONE){
			if (error == SUCCESS){
				//call Leds.led1Toggle();				
				atomic g_state = APP_SET_CHANNEL;
			}else{
				//call Leds.led0Toggle();
			}
		}else{
			//call Leds.led0Toggle();
		}
		
	}
	
	event void AlohaState.stopDone(error_t error){
		if (g_state == APP_WAIT_STOPDONE){
			if (error == SUCCESS){
				//call Leds.led1Toggle();
				atomic g_state = APP_DONE;
				
			}else{
				//call Leds.led0Toggle();
			}
		}else{
			//call Leds.led0Toggle();
		}	

	}
	
	event void AlohaState.setChannelDone(error_t error){
		if (g_state == APP_WAIT_CHDONE){
			if (error == SUCCESS){
				if(TOS_NODE_ID == 0){
					atomic g_state = APP_SEND;
				}else{
					atomic g_state = APP_WAIT_RCV;	
				}
				//atomic g_state = APP_STOP;
			}else{
				//call Leds.led0Toggle();
			}
		}else{
			//call Leds.led0Toggle();
		}	
		
	}
}
